﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace HalconDotNet
{
	/// <summary>
	/// Provides a HALCON window for your Windows Forms application
	/// </summary>
	// Token: 0x0200000D RID: 13
	[ToolboxBitmap(typeof(HWindowControl))]
	[Designer(typeof(HWindowControlDesigner))]
	[DefaultEvent("HMouseMove")]
	public class HWindowControl : UserControl
	{
		// Token: 0x06000054 RID: 84 RVA: 0x0000353C File Offset: 0x0000173C
		public HWindowControl()
		{
			this.InitializeComponent();
			if (HalconAPI.isWindows)
			{
				this.createWindow(false);
			}
			this.paintEventDelegate = new PaintEventHandler(this.HWindowControl_Paint);
			base.Paint += this.paintEventDelegate;
		}

		// Token: 0x06000055 RID: 85 RVA: 0x000035C8 File Offset: 0x000017C8
		private void HWindowControl_Paint(object sender, PaintEventArgs e)
		{
			base.Paint -= this.paintEventDelegate;
			if (!HalconAPI.isWindows)
			{
				this.createWindow(false);
				try
				{
					((Form)base.TopLevelControl).Closing += this.Form_Closing;
				}
				catch (Exception)
				{
				}
			}
			this.OnHInitWindow();
		}

		// Token: 0x06000056 RID: 86 RVA: 0x00003628 File Offset: 0x00001828
		private void Form_Closing(object sender, CancelEventArgs e)
		{
			base.Dispose();
		}

		// Token: 0x06000057 RID: 87 RVA: 0x00003630 File Offset: 0x00001830
		private void HWindowControl_VisibleChanged(object sender, EventArgs e)
		{
			if (this.window != null && base.Visible && this.hwnd != base.Handle)
			{
				this.createWindow(true);
			}
		}

		// Token: 0x06000058 RID: 88 RVA: 0x0000365C File Offset: 0x0000185C
		private void createWindow(bool repair)
		{
			this.BackColor = this.BorderColor;
			if ((this.window == null || repair) && LicenseManager.UsageMode != LicenseUsageMode.Designtime)
			{
				try
				{
					HOperatorSet.SetCheck("~father");
					if (this.window == null)
					{
						this.window = new HWindow();
					}
					else
					{
						int num;
						int num2;
						int num3;
						int num4;
						this.window.GetPart(out num, out num2, out num3, out num4);
						this.imagePart = new Rectangle(num2, num, num4 - num2 + 1, num3 - num + 1);
					}
					this.hwnd = base.Handle;
					this.window.OpenWindow(this.borderWidth, this.borderWidth, base.Width - 2 * this.borderWidth, base.Height - 2 * this.borderWidth, this.hwnd, "visible", "");
					this.UpdatePart();
				}
				catch (HOperatorException ex)
				{
					int errorCode = ex.GetErrorCode();
					if (errorCode >= 5100 && errorCode < 5200)
					{
						throw ex;
					}
				}
				catch (DllNotFoundException)
				{
				}
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		// Token: 0x06000059 RID: 89 RVA: 0x00003778 File Offset: 0x00001978
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.window != null)
			{
				this.window.Dispose();
				this.window = null;
				if (this.components != null)
				{
					this.components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		// Token: 0x0600005A RID: 90 RVA: 0x000037B4 File Offset: 0x000019B4
		private void InitializeComponent()
		{
			base.Name = "HWindowControl";
			base.Size = new Size(320, 240);
			base.VisibleChanged += this.HWindowControl_VisibleChanged;
			base.Resize += this.HWindowControl_Resize;
			base.MouseUp += this.HWindowControl_MouseUp;
			base.MouseMove += this.HWindowControl_MouseMove;
			base.MouseWheel += this.HWindowControl_MouseWheel;
			base.MouseDown += this.HWindowControl_MouseDown;
		}

		// Token: 0x1700000C RID: 12
		// (get) Token: 0x0600005B RID: 91 RVA: 0x0000384D File Offset: 0x00001A4D
		protected override Size DefaultSize
		{
			get
			{
				return new Size(320, 240);
			}
		}

		// Token: 0x0600005C RID: 92 RVA: 0x00003860 File Offset: 0x00001A60
		private void UpdateWindowExtents()
		{
			this.windowExtents = new Rectangle(this.borderWidth, this.borderWidth, base.ClientSize.Width - 2 * this.borderWidth, base.ClientSize.Height - 2 * this.borderWidth);
			if (this.window != null && this.windowExtents.Width > 0 && this.windowExtents.Height > 0)
			{
				int x;
				int y;
				int width;
				int height;
				this.window.GetWindowExtents(out x, out y, out width, out height);
				if (!this.windowExtents.Equals(new Rectangle(x, y, width, height)))
				{
					this.window.SetWindowExtents(this.windowExtents.Left, this.windowExtents.Top, this.windowExtents.Width, this.windowExtents.Height);
					if (HSystem.GetSystem(new HTuple("flush_graphic")).S == "true")
					{
						this.Refresh();
						return;
					}
				}
			}
			else
			{
				this.Refresh();
			}
		}

		// Token: 0x0600005D RID: 93 RVA: 0x00003978 File Offset: 0x00001B78
		private void UpdatePart()
		{
			if (this.window != null)
			{
				this.window.SetPart(this.imagePart.Top, this.imagePart.Left, this.imagePart.Top + this.imagePart.Height - 1, this.imagePart.Left + this.imagePart.Width - 1);
			}
		}

		// Token: 0x0600005E RID: 94 RVA: 0x000039E0 File Offset: 0x00001BE0
		private void HWindowControl_Resize(object sender, EventArgs e)
		{
			this.UpdateWindowExtents();
		}

		/// <summary>
		///   Size of the HALCON window in pixels.
		///   Without border, this will be identical to the control size.
		/// </summary>
		// Token: 0x1700000D RID: 13
		// (get) Token: 0x0600005F RID: 95 RVA: 0x000039E8 File Offset: 0x00001BE8
		// (set) Token: 0x06000060 RID: 96 RVA: 0x000039F5 File Offset: 0x00001BF5
		[Category("Layout")]
		[Description("Size of the HALCON window in pixels. Without border, this will be identical to the control size")]
		public Size WindowSize
		{
			get
			{
				return this.windowExtents.Size;
			}
			set
			{
				base.ClientSize = new Size(value.Width + 2 * this.borderWidth, value.Height + 2 * this.borderWidth);
			}
		}

		/// <summary>
		///  This rectangle specifies the image part to be displayed.
		///  The method SetFullImagePart() will adapt this property to
		///  show the full image.
		/// </summary>
		// Token: 0x1700000E RID: 14
		// (get) Token: 0x06000061 RID: 97 RVA: 0x00003A24 File Offset: 0x00001C24
		// (set) Token: 0x06000062 RID: 98 RVA: 0x00003A6C File Offset: 0x00001C6C
		[Category("Layout")]
		[Description("This rectangle specifies the image part to be displayed, which will automatically be zoomed to fill the window. To display a full image of size W x H, set this to 0;0;W;H")]
		public Rectangle ImagePart
		{
			get
			{
				if (this.window != null)
				{
					int num;
					int num2;
					int num3;
					int num4;
					this.window.GetPart(out num, out num2, out num3, out num4);
					this.imagePart = new Rectangle(num2, num, num4 - num2 + 1, num3 - num + 1);
				}
				return this.imagePart;
			}
			set
			{
				if (value.IsEmpty)
				{
					this.imagePart = new Rectangle(0, 0, base.Width - 2 * this.borderWidth, base.Height - 2 * this.BorderWidth);
				}
				else
				{
					this.imagePart = value;
				}
				this.UpdatePart();
			}
		}

		/// <summary>
		///   Width of optional border in pixels
		/// </summary>
		// Token: 0x1700000F RID: 15
		// (get) Token: 0x06000063 RID: 99 RVA: 0x00003ABC File Offset: 0x00001CBC
		// (set) Token: 0x06000064 RID: 100 RVA: 0x00003AC4 File Offset: 0x00001CC4
		[Category("Appearance")]
		[DefaultValue(0)]
		[Description("Width of optional border in pixels")]
		public int BorderWidth
		{
			get
			{
				return this.borderWidth;
			}
			set
			{
				this.borderWidth = value;
				this.UpdateWindowExtents();
			}
		}

		/// <summary>
		///   Color of optional border around window
		/// </summary>
		// Token: 0x17000010 RID: 16
		// (get) Token: 0x06000065 RID: 101 RVA: 0x00003AD3 File Offset: 0x00001CD3
		// (set) Token: 0x06000066 RID: 102 RVA: 0x00003ADB File Offset: 0x00001CDB
		[Category("Appearance")]
		[Description("Color of optional border around window")]
		public Color BorderColor
		{
			get
			{
				return this.borderColor;
			}
			set
			{
				this.borderColor = value;
				this.BackColor = this.borderColor;
			}
		}

		// Token: 0x17000011 RID: 17
		// (get) Token: 0x06000067 RID: 103 RVA: 0x00003AF0 File Offset: 0x00001CF0
		[Browsable(false)]
		public HWindow HalconWindow
		{
			get
			{
				if (this.window != null)
				{
					return this.window;
				}
				return new HWindow();
			}
		}

		// Token: 0x17000012 RID: 18
		// (get) Token: 0x06000068 RID: 104 RVA: 0x00003B06 File Offset: 0x00001D06
		[Browsable(false)]
		public IntPtr HalconID
		{
			get
			{
				if (this.window != null)
				{
					return this.window.Handle;
				}
				return IntPtr.Zero;
			}
		}

		// Token: 0x17000013 RID: 19
		// (get) Token: 0x06000069 RID: 105 RVA: 0x00003B21 File Offset: 0x00001D21
		// (set) Token: 0x0600006A RID: 106 RVA: 0x00003B29 File Offset: 0x00001D29
		[Browsable(false)]
		public override Color ForeColor
		{
			get
			{
				return base.ForeColor;
			}
			set
			{
				base.ForeColor = value;
			}
		}

		// Token: 0x17000014 RID: 20
		// (get) Token: 0x0600006B RID: 107 RVA: 0x00003B32 File Offset: 0x00001D32
		// (set) Token: 0x0600006C RID: 108 RVA: 0x00003B3A File Offset: 0x00001D3A
		[Browsable(false)]
		public override Color BackColor
		{
			get
			{
				return base.BackColor;
			}
			set
			{
				base.BackColor = value;
			}
		}

		// Token: 0x17000015 RID: 21
		// (get) Token: 0x0600006D RID: 109 RVA: 0x00003B43 File Offset: 0x00001D43
		// (set) Token: 0x0600006E RID: 110 RVA: 0x00003B4B File Offset: 0x00001D4B
		[Browsable(false)]
		public override Image BackgroundImage
		{
			get
			{
				return base.BackgroundImage;
			}
			set
			{
				base.BackgroundImage = value;
			}
		}

		/// <summary>
		///   Adapt ImagePart to show the full image.
		/// </summary>
		/// <param name="reference"></param>
		// Token: 0x0600006F RID: 111 RVA: 0x00003B54 File Offset: 0x00001D54
		public void SetFullImagePart(HImage reference)
		{
			string text;
			int width;
			int height;
			reference.GetImagePointer1(out text, out width, out height);
			this.ImagePart = new Rectangle(0, 0, width, height);
		}

		/// <summary>
		///   Occurs after the HALCON window has been initialized
		/// </summary>
		/// <remarks>
		///   Under Mono/Linux, the HALCON window cannot be initialized
		///   before the Form is visible. Therefore, accessing the window
		///   in the event Load of the Form is not portable.
		/// </remarks>
		// Token: 0x14000009 RID: 9
		// (add) Token: 0x06000070 RID: 112 RVA: 0x00003B80 File Offset: 0x00001D80
		// (remove) Token: 0x06000071 RID: 113 RVA: 0x00003BB8 File Offset: 0x00001DB8
		[Description("Occurs after the HALCON window has been initialized.")]
		[Category("Behavior")]
		public event HInitWindowEventHandler HInitWindow;

		/// <summary>
		///   Occurs when the mouse is moved over the HALCON window. Note that
		///   delta is meaningless here.
		/// </summary>
		// Token: 0x1400000A RID: 10
		// (add) Token: 0x06000072 RID: 114 RVA: 0x00003BF0 File Offset: 0x00001DF0
		// (remove) Token: 0x06000073 RID: 115 RVA: 0x00003C28 File Offset: 0x00001E28
		[Description("Occurs when the mouse is moved over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
		[Category("Mouse")]
		public event HMouseEventHandler HMouseMove;

		/// <summary>
		///   Occurs when a button is pressed over the HALCON window. Note that
		///   delta is meaningless here.
		/// </summary>
		// Token: 0x1400000B RID: 11
		// (add) Token: 0x06000074 RID: 116 RVA: 0x00003C60 File Offset: 0x00001E60
		// (remove) Token: 0x06000075 RID: 117 RVA: 0x00003C98 File Offset: 0x00001E98
		[Description("Occurs when a button is pressed over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
		[Category("Mouse")]
		public event HMouseEventHandler HMouseDown;

		/// <summary>
		///   Occurs when a button is released over the HALCON window. Note that
		///   delta is meaningless here.
		/// </summary>
		// Token: 0x1400000C RID: 12
		// (add) Token: 0x06000076 RID: 118 RVA: 0x00003CD0 File Offset: 0x00001ED0
		// (remove) Token: 0x06000077 RID: 119 RVA: 0x00003D08 File Offset: 0x00001F08
		[Description("Occurs when a button is released over the HALCON window. Note that delta is meaningless here. The position is returned in the image coordinate system.")]
		[Category("Mouse")]
		public event HMouseEventHandler HMouseUp;

		/// <summary>
		///   Occurs when the wheel is used over the HALCON window.  Note that
		///   button is meaningless here.
		/// </summary>
		// Token: 0x1400000D RID: 13
		// (add) Token: 0x06000078 RID: 120 RVA: 0x00003D40 File Offset: 0x00001F40
		// (remove) Token: 0x06000079 RID: 121 RVA: 0x00003D78 File Offset: 0x00001F78
		[Description("Occurs when the wheel is used over the HALCON window. Note that button is meaningless here. The position is returned in the image coordinate system.")]
		[Category("Mouse")]
		public event HMouseEventHandler HMouseWheel;

		// Token: 0x0600007A RID: 122 RVA: 0x00003DAD File Offset: 0x00001FAD
		protected virtual void OnHInitWindow()
		{
			if (this.HInitWindow != null)
			{
				this.HInitWindow(this, new EventArgs());
			}
		}

		// Token: 0x0600007B RID: 123 RVA: 0x00003DC8 File Offset: 0x00001FC8
		protected virtual void OnHMouseMove(HMouseEventArgs e)
		{
			if (this.HMouseMove != null)
			{
				this.HMouseMove(this, e);
			}
		}

		// Token: 0x0600007C RID: 124 RVA: 0x00003DDF File Offset: 0x00001FDF
		protected virtual void OnHMouseDown(HMouseEventArgs e)
		{
			if (this.HMouseDown != null)
			{
				this.HMouseDown(this, e);
			}
		}

		// Token: 0x0600007D RID: 125 RVA: 0x00003DF6 File Offset: 0x00001FF6
		protected virtual void OnHMouseUp(HMouseEventArgs e)
		{
			if (this.HMouseUp != null)
			{
				this.HMouseUp(this, e);
			}
		}

		// Token: 0x0600007E RID: 126 RVA: 0x00003E0D File Offset: 0x0000200D
		protected virtual void OnHMouseWheel(HMouseEventArgs e)
		{
			if (this.HMouseWheel != null)
			{
				this.HMouseWheel(this, e);
			}
		}

		// Token: 0x0600007F RID: 127 RVA: 0x00003E24 File Offset: 0x00002024
		private HMouseEventArgs ToHMouse(MouseEventArgs e)
		{
			double y;
			double x;
			if (this.window == null)
			{
				y = (double)this.imagePart.Top + (double)(e.Y - this.borderWidth) * (double)this.imagePart.Height / (double)this.windowExtents.Height;
				x = (double)this.imagePart.Left + (double)(e.X - this.borderWidth) * (double)this.imagePart.Width / (double)this.windowExtents.Width;
			}
			else
			{
				this.window.ConvertCoordinatesWindowToImage((double)(e.Y - this.borderWidth), (double)(e.X - this.borderWidth), out y, out x);
			}
			return new HMouseEventArgs(e.Button, e.Clicks, x, y, e.Delta);
		}

		// Token: 0x06000080 RID: 128 RVA: 0x00003EEB File Offset: 0x000020EB
		private void HWindowControl_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.windowExtents.Contains(e.X, e.Y))
			{
				this.OnHMouseMove(this.ToHMouse(e));
			}
		}

		// Token: 0x06000081 RID: 129 RVA: 0x00003F13 File Offset: 0x00002113
		private void HWindowControl_MouseDown(object sender, MouseEventArgs e)
		{
			if (this.windowExtents.Contains(e.X, e.Y))
			{
				this.OnHMouseDown(this.ToHMouse(e));
			}
		}

		// Token: 0x06000082 RID: 130 RVA: 0x00003F3B File Offset: 0x0000213B
		private void HWindowControl_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.windowExtents.Contains(e.X, e.Y))
			{
				this.OnHMouseUp(this.ToHMouse(e));
			}
		}

		// Token: 0x06000083 RID: 131 RVA: 0x00003F63 File Offset: 0x00002163
		private void HWindowControl_MouseWheel(object sender, MouseEventArgs e)
		{
			if (this.windowExtents.Contains(e.X, e.Y))
			{
				this.OnHMouseWheel(this.ToHMouse(e));
			}
		}

		// Token: 0x04000043 RID: 67
		private IntPtr hwnd = IntPtr.Zero;

		// Token: 0x04000044 RID: 68
		private HWindow window;

		// Token: 0x04000045 RID: 69
		private Rectangle imagePart = new Rectangle(0, 0, 640, 480);

		// Token: 0x04000046 RID: 70
		private Rectangle windowExtents = new Rectangle(0, 0, 320, 240);

		// Token: 0x04000047 RID: 71
		private int borderWidth;

		// Token: 0x04000048 RID: 72
		private Color borderColor = Color.Black;

		// Token: 0x04000049 RID: 73
		private PaintEventHandler paintEventDelegate;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		// Token: 0x0400004A RID: 74
		private Container components;

		// Token: 0x0400004B RID: 75
		private const string positionDescription = " The position is returned in the image coordinate system.";
	}
}
